Tables with gtExtras

Author

Jason Pemberton

gtExtras

# this one doesn't work
# iris %>% gt_plt_summary()

Visualizing summary statistics table

mtcars %>% 
  group_by(cyl) %>% 
  summarize(Median = round(median(mpg), 1),
            Mean = round(mean(mpg), 1),
            Distribution = list(mpg)) %>% 
  gt() %>% 
  gt_plt_dist(Distribution) %>% 
  gt_theme_guardian() %>% 
  tab_header(title = "Miles per Gallon Statistics")
Miles per Gallon Statistics
cyl Median Mean Distribution
4 26.0 26.7
6 19.7 19.7
8 15.2 15.1

Tips and Tricks

plot <- gapminder %>% 
  rename(Country = country) %>% 
  filter(continent == "Europe") %>% 
  group_by(Country) %>% 
  summarise(`GDP pc` = round(mean(gdpPercap)),
            `Pop size` = round(mean(pop)),
            `Life Expectancy` = list(lifeExp)) %>% 
  arrange(-`GDP pc`) %>% 
  head(10) %>% 
  gt() %>% 
  gt_plt_dist(`Life Expectancy`) %>% 
  tab_header(title = "GDP and Population Size - Europe") %>% 
  cols_align(align = "left")
  
plot
GDP and Population Size - Europe
Country GDP pc Pop size Life Expectancy
Switzerland 27074 6384293
Norway 26747 4031441
Netherlands 21749 13786798
Denmark 21672 4994187
Germany 20557 77547043
Iceland 20531 226978
Austria 20412 7583298
Sweden 19943 8220029
Belgium 19901 9725119
United Kingdom 19380 56087801
plot %>% 
  gt_theme_538()
GDP and Population Size - Europe
Country GDP pc Pop size Life Expectancy
Switzerland 27074 6384293
Norway 26747 4031441
Netherlands 21749 13786798
Denmark 21672 4994187
Germany 20557 77547043
Iceland 20531 226978
Austria 20412 7583298
Sweden 19943 8220029
Belgium 19901 9725119
United Kingdom 19380 56087801
plot %>% 
  gt_theme_dark()
GDP and Population Size - Europe
Country GDP pc Pop size Life Expectancy
Switzerland 27074 6384293
Norway 26747 4031441
Netherlands 21749 13786798
Denmark 21672 4994187
Germany 20557 77547043
Iceland 20531 226978
Austria 20412 7583298
Sweden 19943 8220029
Belgium 19901 9725119
United Kingdom 19380 56087801
plot %>% 
  gt_theme_espn()
GDP and Population Size - Europe
Country GDP pc Pop size Life Expectancy
Switzerland 27074 6384293
Norway 26747 4031441
Netherlands 21749 13786798
Denmark 21672 4994187
Germany 20557 77547043
Iceland 20531 226978
Austria 20412 7583298
Sweden 19943 8220029
Belgium 19901 9725119
United Kingdom 19380 56087801
plot %>% 
  gt_theme_guardian()
GDP and Population Size - Europe
Country GDP pc Pop size Life Expectancy
Switzerland 27074 6384293
Norway 26747 4031441
Netherlands 21749 13786798
Denmark 21672 4994187
Germany 20557 77547043
Iceland 20531 226978
Austria 20412 7583298
Sweden 19943 8220029
Belgium 19901 9725119
United Kingdom 19380 56087801
plot %>% 
  gt_theme_pff()
GDP and Population Size - Europe
Country GDP pc Pop size Life Expectancy
Switzerland 27074 6384293
Norway 26747 4031441
Netherlands 21749 13786798
Denmark 21672 4994187
Germany 20557 77547043
Iceland 20531 226978
Austria 20412 7583298
Sweden 19943 8220029
Belgium 19901 9725119
United Kingdom 19380 56087801
plot <- plot %>% 
  gt_theme_pff()

plot %>% 
  gt_highlight_rows(rows = Country %in% c("Iceland",
                                          "Belgium"),
                    fill = "lightpink")
GDP and Population Size - Europe
Country GDP pc Pop size Life Expectancy
Switzerland 27074 6384293
Norway 26747 4031441
Netherlands 21749 13786798
Denmark 21672 4994187
Germany 20557 77547043
Iceland 20531 226978
Austria 20412 7583298
Sweden 19943 8220029
Belgium 19901 9725119
United Kingdom 19380 56087801
p <- plot %>%
  gt_plt_bar_pct(`GDP pc`,
                  fill = "steelblue",
                 height = 15,
                 width = 100)

p
GDP and Population Size - Europe
Country GDP pc Pop size Life Expectancy
Switzerland
6384293
Norway
4031441
Netherlands
13786798
Denmark
4994187
Germany
77547043
Iceland
226978
Austria
7583298
Sweden
8220029
Belgium
9725119
United Kingdom
56087801
d <- plot %>% 
  gt_theme_pff() %>% 
  gt_color_rows(column = `Pop size`,
                palette = "Pastel1") %>% 
  gt_plt_bar_pct(`GDP pc`,
                 fill = "steelblue",
                 height = 15,
                 width = 100)

d
GDP and Population Size - Europe
Country GDP pc Pop size Life Expectancy
Switzerland
6384293
Norway
4031441
Netherlands
13786798
Denmark
4994187
Germany
77547043
Iceland
226978
Austria
7583298
Sweden
8220029
Belgium
9725119
United Kingdom
56087801